package cn.iocoder.yudao.module.book.dal.mysql.records;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.controller.app.records.vo.AppRecordsRespVO;
import cn.iocoder.yudao.module.book.controller.app.user.vo.UserReadingStatsRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.records.RecordsDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.book.controller.admin.records.vo.*;
import org.apache.ibatis.annotations.Param;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.PastOrPresent;

/**
 * 阅读记录 Mapper
 *
 * @author xi
 */
@Mapper
public interface RecordsMapper extends BaseMapperX<RecordsDO> {

    default PageResult<RecordsDO> selectPage(RecordsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecordsDO>()
                .eqIfPresent(RecordsDO::getRecordId, reqVO.getRecordId())
                .eqIfPresent(RecordsDO::getUserId, reqVO.getUserId())
                .eqIfPresent(RecordsDO::getBookId, reqVO.getBookId())
                .eqIfPresent(RecordsDO::getCollectionId, reqVO.getCollectionId())
                .betweenIfPresent(RecordsDO::getReadingDate, reqVO.getReadingDate())
                .eqIfPresent(RecordsDO::getReadingCount, reqVO.getReadingCount())
                .eqIfPresent(RecordsDO::getReadingNotes, reqVO.getReadingNotes())
                .betweenIfPresent(RecordsDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecordsDO::getRecordId));
    }

    int deleteByBookId(@Param("id") Integer id);

    int deleteByCollectionId(@Param("id") Integer id);

    List<RecordsDO> selectByUserId(@Param("userId") Long userId);

    List<RecordsDO> selectByUserIdAndCreateTimeAfter(@Param("userId") Long userId,@Param("sevenDaysAgo") LocalDateTime sevenDaysAgo);

    RecordsDO selectByBookId(@Param("bookId") Integer bookId, @Param("userId") Long userId);

    void updateTime(@Param("recordId") Integer recordId,@Param("updateTime") @NotNull(message = "阅读日期不能为空") @PastOrPresent(message = "阅读时间不能超过当前时间") LocalDateTime updateTime);

    void updateDeleted(@Param("recordId") Integer recordId, @Param("deleted") boolean b);

    List<RecordsDO> selectDayRecord(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 统计用户今日阅读数量
     */
    Integer countTodayRead(@Param("userId") Long userId, @Param("today") String today);

    /**
     * 统计用户本周阅读数量
     */
    Integer countWeekRead(@Param("userId") Long userId, @Param("weekStart") String weekStart);

    /**
     * 统计用户阅读天数
     */
    Integer countReadDays(@Param("userId") Long userId);

    /**
     * 获取用户最近阅读记录（带绘本信息）
     */
    List<UserReadingStatsRespVO.ReadingRecordItem> selectRecentReadingWithBooks(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 获取用户今日阅读记录（带绘本信息）
     */
    List<UserReadingStatsRespVO.ReadingRecordItem> selectTodayReadingWithBooks(@Param("userId") Long userId, @Param("today") String today);

    /**
     * 统计用户本月阅读数量
     */
    Integer countMonthRead(@Param("userId") Long userId, @Param("monthStart") String monthStart);

    /**
     * 统计用户阅读总数量
     */
    Integer countTotalRead(@Param("userId") Long userId);
}
